﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerMonitoringAssistantWF
{
    public partial class MonitoringWindowUC : UserControl
    {
        private decimal countDown;
        private Timer refreshTimer;
        public MonitoringWindowUC()
        {
            InitializeComponent();
            cmsInterval.ItemClicked += CmsInterval_ItemClicked;
            cmsUrl.ItemClicked += CmsUrl_ItemClicked;
            toolTip1.SetToolTip(tb_interval, "请打开右键菜单展开更多功能!");
        }

        private void CmsInterval_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "停止ToolStripMenuItem":
                    tb_interval.Text = "0";
                    break;
                case "秒5ToolStripMenuItem":
                    tb_interval.Text = "5";
                    break;
                case "秒30ToolStripMenuItem":
                    tb_interval.Text = "30";
                    break;
                case "分钟ToolStripMenuItem":
                    tb_interval.Text = "60";
                    break;
                case "分钟10ToolStripMenuItem":
                    tb_interval.Text = "600";
                    break;
                case "分钟30ToolStripMenuItem":
                    tb_interval.Text = "1800";
                    break;
                case "设置ToolStripMenuItem":
                    FrmTimeSetting frmTime=new FrmTimeSetting() { StartPosition=FormStartPosition.CenterScreen };
                    frmTime.SetTimeHandler += (time) => { countDown = time; tb_interval.Text = time.ToString();  };
                    frmTime.ShowDialog();
                    break;
            }
        }
        private void CmsUrl_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "复制ToolStripMenuItem":
                    tb_url.SelectAll();
                    tb_url.Copy();
                    break;
                case "粘帖并跳转ToolStripMenuItem":
                    tb_url.Paste();
                    if (tb_url.TextLength > 3)
                    {
                        webMain.DocumentText = null;
                        webMain.Navigate(tb_url.Text);
                    }
                    break;
                case "清空ToolStripMenuItem":
                    tb_url.Clear();
                    break;
                case "在浏览器打开ToolStripMenuItem":
                    if (tb_url.TextLength > 3)
                    {
                        System.Diagnostics.Process.Start(tb_url.Text);
                    }
                    break;
                case "粘帖ToolStripMenuItem":
                    tb_url.Paste();
                    break;
            }
        }

        private void btn_go_Click(object sender, EventArgs e)
        {
            if (tb_url.TextLength > 3)
            {
                webMain.DocumentText = null;
                webMain.Navigate(tb_url.Text);
            }
        }

        public void SetInterval(int seconds)
        {
            if (seconds >= 0)
            {
                tb_interval.Text = seconds.ToString();
                countDown = seconds;
                StartTimer();
            }
        }
        public void SetTitle(string title)
        {
            this.lbl_title.Text = title;
        }
        public void SetUrl(string url, int seconds=0)
        {
            tb_url.Text = url;
            webMain.Navigate(url);
            toolTip1.SetToolTip(tb_url, url);
            if (seconds > 0)
            {
                SetInterval(seconds);
            }
        }
        private void StartTimer()
        {
            if (null == refreshTimer)
            {
                refreshTimer = new Timer() { Interval=1000};
                refreshTimer.Tick += (object sender, EventArgs e) =>
                {
                    if (AppUtil.parseToInt(tb_interval.Text) == 0)
                    {
                        countDown = 0;
                        lbl_counter.Text = countDown.ToString();
                        return;
                    }
                    lbl_counter.Text = countDown.ToString();
                    if (countDown == 0 && tb_url.TextLength > 0)
                    {
                        webMain.Navigate(tb_url.Text);
                    }
                    countDown--;
                    if (countDown < 0)
                    {
                        countDown = parseToInt(tb_interval.Text);
                    }
                };
            }
            refreshTimer.Start();
        }
        int parseToInt(string val)
        {
            int iout;
            return int.TryParse(val, out iout) ? iout : 0;
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            webMain.DocumentText = null;
            webMain.Refresh();
        }
    }
}
